<template>
  <div>
    <div class="swiper-container">
      <div class="swiper-wrapper">
        <div
          class="swiper-slide"
          v-for="banner in banners"
          :key="banner.id"
        >
          <img :src="banner.imageUrl" alt="">
        </div>
      </div>
      <div class="swiper-pagination"></div>
      <div class="swiper-button-prev"></div>
      <div class="swiper-button-next"></div>
    </div>
  </div>
</template>

<script>
import Swiper from 'swiper'
import 'swiper/css/swiper.min.css'

export default {
  name: 'home',
  data () {
    return {
      banners: []
    }
  },
  created () {
    this.getBanners()
  },
  methods: {
    getBanners () {
      this.$http.getHome()
        .then(resp => {
          this.banners = resp.banners
          this.$nextTick()
            .then(() => {
              // eslint-disable-next-line
              new Swiper('.swiper-container', {
                loop: true,
                pagination: {
                  el: '.swiper-pagination'
                },
                navigation: {
                  nextEl: '.swiper-button-next',
                  prevEl: '.swiper-button-prev'
                }
              })
            })
        })
    }
  }
}
</script>

<style lang="scss">
  .swiper-container {
    position: relative;
    width: 100%;
    // 元素的padding-top和padding-bottom的百分比是按当前元素的宽度来计算的
    padding-top: 378 / 1008 * 100%;
  }
  .swiper-wrapper {
    position: absolute;
    left: 0;
    top: 0;
    img {
      width: 100%;
    }
  }
</style>
